package adventure.commands;

import adventure.*;
import player.*;
import colour.ColourUtil;

/**
 *
 * @author Michael Hanns
 *
 */
public class Close extends AbstractCommand {

    public Close(Adventure a) {
        super(a, new String[]{"CLOSE"});
    }

    @Override
    public String validateAndExecute(Player p, String[] args) {
        if (args.length == 1) {
            return "Close where?";
        } else if (args.length == 2 && Util.isDirection(args[1])) {
            return close(p, args[1].charAt(0));
        } else {
            return "That is not a valid direction!";
        }
    }

    private String close(Player player, char dir) {
        int x = player.x();
        int y = player.y();
        int z = player.z();

        if (!rooms.isClosed(x, y, z, dir)) {
            if (rooms.closeableExit(x, y, z, dir)) {
                rooms.close(x, y, z, dir);

                out.broadcastLocalToPlayer(player,
                        ColourUtil.colourise(player.getName(), ColourUtil.magenta)
                        + " closes the " + ColourUtil.colourise(Util.fillDir(dir), ColourUtil.yellow) + " door.");


                return "You close the " + ColourUtil.colourise(Util.fillDir(dir), ColourUtil.yellow) + " door.";
            } else {
                return "That way is not closeable!";
            }
        } else {
            return "That door is already closed!";
        }
    }
}
